#if CONFIG_IIC_DRIVER_TESTER
#include "IicDriverTester.h"
#include "Debug.h"

void IicDriverTester_Init(IicDriver* pIicDriver, uint8 channel, Queue* pRxQueue, Queue* pTxQueue, IicDriverCallBackFun callBack, void* pCallBackObj)
{
	Trace("%s(channel=%d)\n", _FUNC_, channel);
	memset(pIicDriver, 0, sizeof(IicDriver));
	
	pIicDriver->m_Channel = channel;
	pIicDriver->m_pRxQueue = pRxQueue;
	pIicDriver->m_pTxQueue	= pTxQueue;

	pIicDriver->m_Base.TxData = (TransferFun)IicDriverTester_TxData;

	QUEUE_init(&pIicDriver->m_EventQueue, (uint8*)pIicDriver->m_pEventQueueBuff, sizeof(uint32), MAX_BUS_EVENT);

	pIicDriver->CallBack = callBack;
	pIicDriver->m_pCallBackObj = pCallBackObj;
}

void IicDriverTester_Reset(IicDriver* pIicDriver)
{
	QUEUE_reset(pIicDriver->m_pRxQueue);
	QUEUE_reset(pIicDriver->m_pTxQueue);
	QUEUE_reset(&pIicDriver->m_EventQueue);
}

int IicDriverTester_TxData(IicDriver* pIicDriver, uint8* pData, uint8 len)
{
	if(QUEUE_isFull(pIicDriver->m_pTxQueue))
	{
		Assert(False);
	}
	else
	{
		BusDataPacket packet;
		
		TRACE_LEVEL(TRACE_IICTRANSFER | TRACE_INTEG_TEST, ("%s(len=%d) Dump Data: ", _FUNC_, len));	
		DUMP_BYTE_LEVEL(TRACE_IICTRANSFER | TRACE_INTEG_TEST, pData, len);	
		
		memcpy(packet.m_Data, pData, len);
		packet.m_Len = len;		
		QUEUE_add(pIicDriver->m_pTxQueue,&packet);

		if(pIicDriver->Notify)
		{
			pIicDriver->Notify(pIicDriver->m_Channel, IIC_TXOK_MASK);
		}
		
		
		return ERR_SUCCESS;
	}
	return ERR_FAILED;
}

int IicDriverTester_TxDataFailed(IicDriver* pIicDriver, uint8* pData, uint8 len)
{
	Trace("IicDriver return ERR_FAILED\n");
	return ERR_FAILED;
}


int IicDriverTester_GetStatus(IicDriver* pIicDriver, uint8* pRxBufStatus, uint8* pTxBufStatus, uint8* pEventStatus)
{	
	if(QUEUE_isFull(pIicDriver->m_pTxQueue))
	{
		*pTxBufStatus = IIC_BUF_FULL;
	}
	else if(QUEUE_isEmpty(pIicDriver->m_pTxQueue))
	{
		*pTxBufStatus = IIC_BUF_EMPT;
	}
	
	if(QUEUE_isFull(pIicDriver->m_pRxQueue))
	{
		*pRxBufStatus = IIC_BUF_FULL;
	}
	else if(QUEUE_isEmpty(pIicDriver->m_pRxQueue))
	{
		*pRxBufStatus = IIC_BUF_EMPT;
	}

	*pEventStatus = 0;

	return 0;
}


int IicDriverTester_RxData(IicDriver* pIicDriver, uint8* pData, uint8 len)
{
	int nRet = len;
	BusDataPacket* packet = (BusDataPacket*)QUEUE_getHead(pIicDriver->m_pRxQueue);

	if(packet)
	{
		if(len >= packet->m_Len)
		{
			nRet = packet->m_Len;
		}

		pIicDriver->CallBack(pIicDriver->m_pCallBackObj, EVENT_RX_SUCCESS, packet->m_Data, packet->m_Len);

		memcpy(pData, packet->m_Data, nRet);
		QUEUE_removeHead(pIicDriver->m_pRxQueue);
	}

	return nRet;
}

void IicDriverTester_Rx(IicDriver* pIicDriver)
{
	BusDataPacket* packet = (BusDataPacket*)QUEUE_getHead(pIicDriver->m_pRxQueue);

	if(packet)
	{
		TRACE_LEVEL(TRACE_IICTRANSFER | TRACE_INTEG_TEST, ("%s() Dump Data: ", _FUNC_));	
		DUMP_BYTE_LEVEL(TRACE_IICTRANSFER | TRACE_INTEG_TEST, (uint8 *)&packet->m_Data, packet->m_Len);	
		pIicDriver->CallBack(pIicDriver->m_pCallBackObj, EVENT_RX_SUCCESS, packet->m_Data, packet->m_Len);
		QUEUE_removeHead(pIicDriver->m_pRxQueue);
	}
}

#endif
